'use strict'

import * as bodyParser from "body-parser";
import * as express from "express";
import * as cors from 'cors';
import * as path from 'path';
import userRouter from './routes/user';

const app = express();

// set request
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// set front page and 404 page
app.use(express.static(path.join(__dirname, 'public')));
// set ejs
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.get("/", (_req, res) => { res.send("server is running!") });
app.use('/ejs_test', (_req, res) => { res.send('index') })
app.use('/user', userRouter);
// TODO: set other router

// set 404
app.use((_req, res, next) => { res.json({ code: 404, data: [], message: '404 not found!' }) })

// start server
app.set("port", process.env.PORT || 4399);
const port = app.get("port");
app.listen(port, () =>
    console.log(`Server started on port ${port}`)
);